package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.hbpa.dao.DaduiLoginDao;
import com.hbpa.form.DaduiLoginAcc;
import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcUtil;

public class DaduiLoginDaoImpl implements DaduiLoginDao {

	public boolean addDaduiAcc(DaduiLoginAcc acc) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("insert into dd_acc(xm,ssdd,pass) values(?,?,?)");
			 
			ps.setString(1, acc.getXm());
			ps.setString(2, acc.getSsdd());
			ps.setString(3, acc.getPass());
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}
	public boolean checkDaduiAccById(String id) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from dd_acc where id = ?");
			ps.setString(1,id);
			
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(count.trim().equals("1")){
					return true;
				}else{
					return false;
				}
			}
			 
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}
	
	
	
	
	public boolean checkDaduiAcc(String xm) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from dd_acc where xm = ?");
			ps.setString(1,xm);
			
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(count.trim().equals("1")){
					return true;
				}else{
					return false;
				}
			}
			 
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}
	
	public boolean checkDaduiAcc(DaduiLoginAcc acc) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from dd_acc where xm = ? and pass = ?");
			 
			ps.setString(1, acc.getXm());
			ps.setString(2, acc.getPass()); 
			 
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(count.trim().equals("1")){
					return true;
				}else{
					return false;
				}
			}
			 
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}

	public boolean modifyDaduiAccPassword(String id, String newPassword) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("update dd_acc set pass = ? where id = ?");
			ps.setString(1, newPassword);
			ps.setString(2, id); 
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
			return false;
	}

	public DaduiLoginAcc getDaduiAcc(String xm) {
		if(this.checkDaduiAcc(xm)==false)
		{
			return null;
		}
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null; 
		DaduiLoginAcc item = new DaduiLoginAcc();
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select * from dd_acc where xm = ? order by id asc");
			 
			ps.setString(1, xm);
			rs = ps.executeQuery();
			if(rs.next())
			{   
				item = new DaduiLoginAcc();
				item.setId(rs.getString("id"));
				item.setPass(rs.getString("pass"));
				item.setSsdd(rs.getString("ssdd")); 
				item.setXm(rs.getString("xm")); 
			}
			 return item;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		} 
		return null;
	}

	
	public boolean modifyDaduiAcc(String id,String xm,String ssdd,String pass)
	{
		Connection con = null;
		Statement st = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			
			st = con.createStatement();
			String sql1 = "update dd_acc set xm = '" + xm + "' where id = "+id;
			String sql2 = "update dd_acc set ssdd = '" + ssdd + "' where id = "+id;
			String sql3 = "update dd_acc set pass = '" + pass + "' where id = "+id;
			 
		    st.executeUpdate(sql1);
		    st.executeUpdate(sql2);
		    st.executeUpdate(sql3);
		  
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(st, con);
		}
			return false;
	}
}
